package com.enjoyor.soa.traffic.rest.efoms.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.efoms.RepairsInfoDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.rest.efoms.helper.IReportHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@Auth
@RestController
@RequestMapping({ "/html" })
@Api("getTZhtmlController相关api")
@CrossOrigin
public class getTZhtmlController {

	@Value("${oracle.ubms.url}")
    private String url;
	
	@Value("${oracle.ubms.username}")
    private String username;
	
	@Value("${oracle.ubms.password}")
    private String password;
	
	@GetMapping(value = { "/getTestHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("参数传递测试（非功能接口）")
	public void getTestHtml(
			@RequestParam(defaultValue = "",value="startPage",required=false) int startPage,
			@RequestParam(defaultValue = "",value="endPage",required=false) int endPage,
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("startPage", startPage);
		map.put("endPage", endPage);
		map.put("path","E://ftpserver//efomsUpload//project_dept.jasper");
		map.put("name", "project_dept");
		exportReport(request,response,"html",map);
	}
	
	@GetMapping(value = { "/getProjectAreaHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("按建设项目-区域获取html")
	public void getProjectAreaHtml(
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("path","E://ftpserver//efomsUpload//project_area.jasper");
		map.put("name", "project_area");
		exportReport(request,response,"html",map);
	}
	
	@GetMapping(value = { "/getProjectDeptHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("按建设项目-部门获取html")
	public void getProjectDeptHtml(
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("path","E://ftpserver//efomsUpload//project_dept.jasper");
		map.put("name", "project_dept");
		exportReport(request,response,"html",map);
	}
	
	@GetMapping(value = { "/getFactoryAreaHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("按生产厂家-区域获取html")
	public void getTest(
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("path","E://ftpserver//efomsUpload//company_area.jasper");
		map.put("name", "company_area");
		exportReport(request,response,"html",map);
	}
	
	@GetMapping(value = { "/getFactoryDeptHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("按生产厂家-部门获取html")
	public void getFactoryDeptHtml(
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("path","E://ftpserver//efomsUpload//company_dept.jasper");
		map.put("name", "company_dept");
		exportReport(request,response,"html",map);
	}
	
	@GetMapping(value = { "/getJSDWAreaHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("按建设单位-区域获取html")
	public void getJSDWAreaHtml(
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("path","E://ftpserver//efomsUpload//JSDW_AREA.jasper");
		map.put("name", "JSDW_AREA");
		exportReport(request,response,"html",map);
	}
	
	@GetMapping(value = { "/getJSDWDeptHtml" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("按建设单位-部门获取html")
	public void getJSDWDeptHtml(
			            HttpServletRequest request,HttpServletResponse response)throws Exception{
		Map<String,Object> map = new HashMap<>();
		map.put("path","E://ftpserver//efomsUpload//jsdw_area.jasper");
		map.put("name", "jsdw_area");
		exportReport(request,response,"html",map);
	}
	
	
	/***
	 * 获取html、exce、word等文件
	 * @param request
	 * @param response
	 * @param rptType 生成文件类型；
	 * @param startPage 开始页数；
	 * @param endPage 结束页数；
	 */
	public void exportReport(HttpServletRequest request,HttpServletResponse response,String rptType,Map map){
		try {
		byte[] bytes = null;
		if(rptType.endsWith("excel")){
		bytes = IReportHelper.GetReportExcel((String) map.get("path"), map, getConn());
		response.setHeader("Content-Disposition","attachment;filename="+map.get("name")+".xls");
		}else if(rptType.endsWith("html")){
		bytes = IReportHelper.GetReportHtml((String) map.get("path"), map, getConn());
		response.setHeader("Content-Disposition","attachment;filename="+map.get("name")+".html");
		}
		response.setContentType("application/ms_word");
		response.setContentLength(bytes.length);
		ServletOutputStream ouputStream = response.getOutputStream();
		ouputStream.write(bytes, 0, bytes.length);
		ouputStream.flush();
		ouputStream.close();
		} catch (Exception e) {
		e.printStackTrace();
		}
		}

		/*
		 * 数据库连接配置；
		 */
		public Connection getConn() {
		Connection connection = null;
		try {
		String driverName = "oracle.jdbc.driver.OracleDriver";
		Class.forName(driverName);
		connection = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
		System.err.println("Could not find the database driver");
		} catch (Exception e) {
		System.err.println("Could not connect to the database");
		}
		return connection;
		}
		
}
